<?php
include("../includes/DBFunctions.php");

function regionReportFunctor($sid) {
	$db = new DBFunctions;
	$db->connectDB();
	$query = "call getPlantInfo(".$sid.",@a);";
	$result = $db->query($query);
	if( !$result ) {
		die("alert('Failed stored procedure call (getPlantInfo)');");
	}
	if (mysqli_num_rows($result) != 1) {
		$db->closeDB();
		$toPrint = "<div><h3>n/a</h3></div>";;
	} else {
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$cName = $line['CommonName'];
			$toPrint = "<div><h3>".$cName." <i>(".$line['ScientificName'].")</i></h3><ul>";
			$toPrint .= "<li>Heat Tolerance: ".$line['MinHeat']." - ".$line['MaxHeat']."&deg; F</li>";
			$toPrint .= "<li>Sunlight Tolerance: ".$line['MinSunlight']." - ".$line['MaxSunlight']." hrs/day</li>";
			$toPrint .= "<li>Precipitation Tolerance: ".$line['MinPrecip']." - ".$line['MaxPrecip']." mm/year</li>";
			$toPrint .= "<li>Weight: ".$line['HungerIndex']." lbs</li>";
			$toPrint .= "</ul></div>";
		}
		$db->closeDB();
	}
	
	echo "displayReport('".$toPrint."');";
}

if( !isset($_POST['sid']) || $_POST['sid'] == '' ) {
	die("alert('No plant was selected. Please try again.');");
}
$sid = $_POST['sid'];
regionReportFunctor($sid);
?>